package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.DateUtils;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.FSchedue;
import com.cookor.poi.modules.phy.entity.FSchedueTemplate;
import com.cookor.poi.modules.phy.service.FSchedueService;
import com.cookor.poi.modules.phy.service.FSchedueTemplateService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/fSchedue"})
public class FSchedueController extends BaseController {
  @Autowired
  private FSchedueService fSchedueService;
  
  @Autowired
  private FSchedueTemplateService fSchedueTemplateService;
  
  @ModelAttribute
  public FSchedue get(@RequestParam(required = false) String id) {
    FSchedue entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.fSchedueService.get(id); 
    if (entity == null)
      entity = new FSchedue(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:fSchedue:view"})
  @RequestMapping({"list", ""})
  public String list(FSchedue fSchedue, HttpServletRequest request, HttpServletResponse response, Model model) {
    List<FSchedueTemplate> templateList = this.fSchedueTemplateService.findList(new FSchedueTemplate());
    if (fSchedue == null)
      fSchedue = new FSchedue(); 
    if (fSchedue.getScheduleMonth() == null)
      fSchedue.setScheduleMonth(Integer.valueOf(Integer.parseInt(DateUtils.getMonth()))); 
    if (fSchedue.getScheduleYear() == null)
      fSchedue.setScheduleYear(Integer.valueOf(Integer.parseInt(DateUtils.getYear()))); 
    Page<FSchedue> page = this.fSchedueService.findPage(new Page(request, response), fSchedue);
    model.addAttribute("fSchedue", fSchedue);
    model.addAttribute("templateList", templateList);
    model.addAttribute("page", page);
    return "modules/phy/fSchedueList";
  }
  
  @RequiresPermissions({"phy:fSchedue:view"})
  @RequestMapping({"form"})
  public String form(FSchedue fSchedue, Model model) {
    model.addAttribute("fSchedue", fSchedue);
    List<FSchedueTemplate> templateList = this.fSchedueTemplateService.findList(new FSchedueTemplate());
    model.addAttribute("templateList", templateList);
    return "modules/phy/fSchedueForm";
  }
  
  @RequiresPermissions({"phy:fSchedue:edit"})
  @RequestMapping({"save"})
  public String save(FSchedue fSchedue, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, fSchedue, new Class[0]))
      return form(fSchedue, model); 
    this.fSchedueService.save(fSchedue);
    addMessage(redirectAttributes, new String[] { "保存排班列表成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/fSchedue/?repage";
  }
  
  @RequiresPermissions({"phy:fSchedue:edit"})
  @RequestMapping({"delete"})
  public String delete(FSchedue fSchedue, RedirectAttributes redirectAttributes) {
    this.fSchedueService.delete(fSchedue);
    addMessage(redirectAttributes, new String[] { "删除排班列表成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/fSchedue/?repage";
  }
}
